package com.example.service.impl;

import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    @Override
    public boolean save(User user) {
        // 检查用户名是否已存在
        User existUser = userMapper.selectByUsername(user.getUsername());
        if (existUser != null) {
            return false; // 用户名已存在
        }
        return userMapper.insert(user) > 0;
    }
    
    @Override
    public boolean update(User user) {
        // 检查用户是否存在
        User existUser = userMapper.selectById(user.getId());
        if (existUser == null) {
            return false; // 用户不存在
        }
        
        // 如果修改了用户名，检查新用户名是否已存在
        if (!existUser.getUsername().equals(user.getUsername())) {
            User userByUsername = userMapper.selectByUsername(user.getUsername());
            if (userByUsername != null) {
                return false; // 新用户名已存在
            }
        }
        
        return userMapper.update(user) > 0;
    }
    
    @Override
    public boolean deleteById(Integer id) {
        return userMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean deleteBatch(List<Integer> ids) {
        return userMapper.deleteBatch(ids) > 0;
    }
    
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }
    
    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }
    
    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
    
    @Override
    public List<User> findByCondition(Map<String, Object> params) {
        return userMapper.selectByCondition(params);
    }
    
    @Override
    public List<User> findByKeyword(String keyword) {
        return userMapper.selectByKeyword(keyword);
    }
    
    @Override
    public Map<String, Object> findPage(Map<String, Object> params) {
        // 计算分页参数
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        
        // 计算偏移量
        Integer offset = (pageNum - 1) * pageSize;
        params.put("offset", offset);
        params.put("pageSize", pageSize);
        
        // 查询数据
        List<User> list = userMapper.selectPage(params);
        int total = userMapper.selectCount(params);
        
        // 封装结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        result.put("pages", (total + pageSize - 1) / pageSize);
        
        return result;
    }
}